package com.situ.edu.filter;


import com.situ.edu.pojo.entity.Auth;
import com.situ.edu.pojo.entity.User;
import com.situ.edu.service.IAuthService;
import com.situ.edu.service.IUserService;
import com.situ.edu.service.impl.AuthServiceImpl;
import com.situ.edu.service.impl.UserServiceImpl;
import com.situ.edu.util.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//关闭过滤器
@WebFilter(filterName = "login",urlPatterns = "/*")
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("LoginFilter.init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("LoginFilter.doFilter");

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        String method = request.getParameter("method");

        if (requestURI.startsWith("/static") //static下可能不止css、js文件，可能有别的文件。
                || requestURI.equals("/page/login")
                || requestURI.equals("/verifyCode")
                || requestURI.equals("/user") && "login".equals(method)){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //账号密码在cookie中找不到
        if (user == null){
            Cookie cookie = CookieUtils.getCookie(request, "autoLogin");
            if (cookie != null){
                //存在自动登录
                //从cookie中找到账号&密码，并且从&开始分割
                String[] values = cookie.getValue().split("&");
                String name = values[0];//账号
                String password = values[1];//密码
                //创建对象UserServiceImpl,
                IUserService userService = new UserServiceImpl();
                //从UserServiceImpl对象login方法中获取账号密码，并把获得的值(数据库中的值)赋值给user
                user = userService.login(name,password);
                //数据库中的值不空
                if (user != null){
                    session.setAttribute("user",user);
                    IAuthService authService = new AuthServiceImpl();
                    List<Auth> auths = authService.selectByRole(user.getRole());
                    System.out.println(auths);
                    session.setAttribute("auths",auths);
                    filterChain.doFilter(servletRequest,servletResponse);
                    return;
                }
            }

            response.sendRedirect("/page/login");
            return;
        }

        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("LoginFilter.destroy");
    }
}
